package com.ciwong.epaper.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.bean.UserPointMedalsBean;
import com.ciwong.epaper.modules.reciteWords.bean.LearnCountBean;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Xml2BitmapUtils {
    private static final String TAG = "Xml2BitmapUtils";

    public static void saveCroppedImage(Context context, LearnCountBean learnCountBeanInfor) {
        Bitmap bmp = makeBitmap(makeViewWithInfor(context, learnCountBeanInfor));
        String shareReciteWordIconLocalPath = FileUtil.makeShareReciteWordIconLocalPath(context);
        File file = new File(shareReciteWordIconLocalPath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void saveCroppedImage(Context context, View shareView,  String filePath) {
        Bitmap bmp = makeBitmap(shareView);
        if (TextUtils.isEmpty(filePath))
            filePath = FileUtil.makeShareIntegralDialogLocalPath(context);
        File file = new File(filePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static Bitmap makeBitmap(View view) {
        Bitmap shareBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(shareBitmap);
        view.draw(c);
        return shareBitmap;
    }

    public static View makeViewWithInfor(Context context, LearnCountBean learnCountBeanInfor) {

        LayoutInflater factorys = LayoutInflater.from(context);
        View view = factorys.inflate(R.layout.view_make_bitmap, null);
        ImageView iv_base_bitmap = view.findViewById(R.id.iv_base_bitmap);
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        SimpleDraweeView iv_mv_head = view.findViewById(R.id.iv_mv_head);
        TextView tv_mv_name = view.findViewById(R.id.tv_mv_name);
        TextView tv_mv_date = view.findViewById(R.id.tv_mv_date);
        TextView tv_mv_days = view.findViewById(R.id.tv_mv_days);
        TextView tv_mv_today_words = view.findViewById(R.id.tv_mv_today_words);
        if (learnCountBeanInfor == null) return view;
        String headUrl = learnCountBeanInfor.getHeadUrl();
        if (!TextUtils.isEmpty(headUrl)) {
            iv_mv_head.setImageURI(Uri.parse(headUrl));
        }
        tv_mv_name.setText(learnCountBeanInfor.getUserName());
        tv_mv_date.setText(learnCountBeanInfor.getDate());
        tv_mv_days.setText(learnCountBeanInfor.getCountDay() + "");
        tv_mv_today_words.setText(learnCountBeanInfor.getDayCount() + "");
        return view;
    }

}
